package com.dumovie.utils;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.storage.model.FileListing;
import com.qiniu.util.Auth;

public class QiniuUtil {
	// bak
		@SuppressWarnings("unused")
		private static final String ACCOUNT_NAME = "account@dumovie.com";
		@SuppressWarnings("unused")
		private static final String ACCOUNT_PWD = "Du*****8";
		
		private static final String ACCESS_KEY = "80YicE4fgmV8YjrCyd16PXWOCIVb0jBpqvKYH9nx";
		private static final String SECRET_KEY = "bbIwARa3oHoacNStjQ3GebFTuzp3qe7xxaI-hC9H";
		private static final String BUCKET = "dumovie";
//		public static final String BUCKET_DOMAIN = "http://o90c45nl6.bkt.clouddn.com/";
		public static final String BUCKET_DOMAIN = "http://res.dumovie.com/";
		
		public static String getToken(){
			Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
			return auth.uploadToken(BUCKET);
		}
		
		public static FileInfo[] getFilesList(String prefix){
			// 设置需要操作的账号的AK和SK
			String ACCESS_KEY = "80YicE4fgmV8YjrCyd16PXWOCIVb0jBpqvKYH9nx";
			String SECRET_KEY = "bbIwARa3oHoacNStjQ3GebFTuzp3qe7xxaI-hC9H";
			Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

			Zone z = Zone.zone0();
			Configuration c = new Configuration(z);

			// 实例化一个BucketManager对象
			BucketManager bucketManager = new BucketManager(auth, c);

			// 要列举文件的空间名
			String bucket = "video";

			try {
				// 调用listFiles方法列举指定空间的指定文件
				// 参数一：bucket 空间名
				// 参数二：prefix 文件名前缀
				// 参数三：marker 上一次获取文件列表时返回的 marker
				// 参数四：limit 每次迭代的长度限制，最大1000，推荐值 100
				// 参数五：delimiter 指定目录分隔符，列出所有公共前缀（模拟列出目录效果）。缺省值为空字符串
				FileListing fileListing = bucketManager.listFiles(bucket, prefix, null, 10, null);
				FileInfo[] items = fileListing.items;
				
				return items;
				
			} catch (QiniuException e) {
				// 捕获异常信息
				Response r = e.response;
				System.out.println(r.toString());
			}
			return null;
		}
}
